#include "Bridge.h"
#include "ShareComponents.h"
#include "RSManager.h"
#include "Box.h"
#include "Sprite.h"
#include "Map.h"
void Bridge::init()
{
	count = 0;
	_delayTime = 0;
	_playerOnTop = false;
	_explode = false;
	this->_sprite = RSManager::getInstance()->getSprite(DAMAGE_BRIDGE);
}

void Bridge::update()
{	

	if(gl_player->getRight() > _box->_position.x)
	{
		_playerOnTop = true;
	}
	if(_explode)
	{
		_delayTime += GAME_TIME;
		if(_delayTime > 120)
		{
			_delayTime = 0;
			_sprite->Next();
			count++;
		}
	}
	if(_playerOnTop && !_explode)
	{
		_delayTime += GAME_TIME;
		if(_delayTime > 600)
		{
			_delayTime = 0;
			int j = int(_box->_position.x / 32.0f);
			gl_map->resetMap(7,j,0);
			gl_map->resetMap(7,j + 1,0);
			gl_map->resetMap(8,j,0);
			gl_map->resetMap(8,j + 1,0);
			this->_box->_size.x = 0;
			this->_box->_size.y = 0;
			x = this->_box->_position.x ;
			y = this->_box->_position.y ;
			this->_box->_position.x = 0;
			this->_box->_position.x = 0;		
			_explode = true;
			
		}
	}	
}



Bridge::Bridge(Box* box) : StaticObject(box,STATIC_ID::BRIDGE)
{
	init();
}

Bridge::~Bridge(void)
{
	
}

void Bridge::render()
{
	if(count > 3) return;
	if(_explode)
	{
		_sprite->Render(D3DXVECTOR2(x,y - 30));		
		_sprite->Render(D3DXVECTOR2(x - 30,y));
		_sprite->Render(D3DXVECTOR2(x + 30,y));
	}
}

Box* Bridge::getBox()
{
 	return this->_box;
}
